#include "Application.h"

#include <string>

#include "Character.h"
#include "EnemyDatabase.h"
#include "ItemDatabase.h"
#include "Cursor.h"
#include "AnimatedSprite.h"
#include "ResourceDatabase.h"

Application::Application()
: exit(false)
, window(sf::RenderWindow(sf::VideoMode(800,600,32), "Shootin' Zombies!"))
{
    //setActiveScreen(introScreen);

    //render(); // Render intro screen whilst stuff loads.

    //setActiveScreen(menuScreen);
    
    // Initialise singletons that don't have to be cleaned up after each game.
    ResourceDatabase::initialise("TestSave");
    ItemDatabase::initialise("TestSave");

    EnemyDatabase::initialise("TestSave");
}

Application::~Application()
{
    // Clean up singletons, etc.    
    EnemyDatabase::cleanUp();
    ItemDatabase::cleanUp();

    removeTempSave();
}

void Application::initialise(CommandArgs args)
{
    activeGame.setWindow(&window);
    activeGame.setActive(true);

    activeGame.newGame("", "");

    // Initialise mouse handler class
    Cursor::get()->init(&window);

}

void Application::run()
{
	while(window.IsOpened())
	{
        eventUpdate(window);
        timeUpdate(window.GetFrameTime() / 1000.0f);
        render(window);
	}
}

void Application::eventUpdate(sf::RenderWindow& window)
{
    sf::Event Event;
	while(window.PollEvent(Event))
	{
		if(Event.Type == sf::Event::Closed)
			window.Close();
	}

    if(/*escape pressed*/0)
    {
        if(activeGame.isActive())
        {
            // Prompt to end game and go back to menu.
        }
        else
        {
            // Prompt to quit application.
        }
    }

    if(activeGame.isActive())
    {
        activeGame.update(window, Event);
    }
    else
    {
        //activeScreen->update(event);
    }
}

void Application::timeUpdate(float delta)
{
    if(activeGame.isActive())
    {
        activeGame.update(delta);
    }
}

void Application::render(sf::RenderWindow& window)
{
    activeGame.render(window);
}

void Application::setActiveScreen(/*screen*/)
{
    /*if(activeScreen)
    {
        activeScreen->Set_Visible(false);
    }

    activeScreen = screen;

    if(activeScreen)
    {
        activeScreen->Set_Visible(true);
    }*/
}

void Application::menu()
{
    //removeTempSave();
    //setActiveScreen(menuScreen);
}

void Application::newGame()
{
    //removeTempSave();
    //activeGame.newGame("_default", "_temp");
    //setActiveScreen(boost::shared_ptr<Base_Screen>());
}

void Application::exitApp()
{
    //removeTempSave();
	//exit = true;
}

void Application::exitToMenu()
{
	//menu();
}

void Application::removeTempSave()
{
    //if(activeGame.getName() == "_temp")
    //{ // "_temp" is the name of temp games - ie it is replaced when saved under another name.
    //    activeGame.Delete();
    //}
    //else
    //{ // Active game isn't the temp game; load temp game and delete it.
    //    Game::deleteGame("_temp");
    //}

    activeGame.deleteActiveGame();
}